
package com.afd.ozblip.activity;

import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.List;

import com.afd.ozblip.adapter.DraftsReviewListAdapter;
import com.afd.ozblip.adapter.DraftsSignListAdapter;
import com.afd.ozblip.adapter.ReviewListAdapter;
import com.afd.ozblip.adapter.SignListAdapter;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.entity.ReviewEntity;
import com.afd.ozblip.entity.SignEntity;
import com.afd.ozblip.util.CollectionUtil;
import com.j256.ormlite.android.AndroidDatabaseConnection;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class UserDraftsActivity extends BaseActivity {

    /****************** 视图变量 **************************/

    private LinearLayout commonRight;
    private RadioGroup tabRb;
    private RadioButton reviewRb;
    private RadioButton signRb;
    private TextView reviewTipTv;
    private TextView signTipTv;
    private ListView reviewListView;
    private ListView signListView;
    private RelativeLayout reviewLayout;
    private RelativeLayout signLayout;
    /****************** 其它变量 **************************/
    private List<ReviewEntity> reviewList;
    private List<SignEntity> signList;
    private DraftsReviewListAdapter reviewListAdapter;
    private DraftsSignListAdapter signListAdapter;
    private OzBlipApplication ozBlipApp;
    private Dao<ReviewEntity, Integer> reviewDao;
    private Dao<SignEntity, Integer> signDao;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drafts);
        // 启动返回功能
        toBack();
        // 寻找视图
        findViews();
        // 设置监听
        setListeners();
        // 获取参数
        getBundle();
    }

    private LinearLayout commonLeft;

    /**
     * 返回上一个界面
     */
    public void toBack() {
        commonLeft = (LinearLayout) findViewById(R.id.ll_common_left);
        commonLeft.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    /**
     * 寻找视图
     */
    public void findViews() {
        // 初始化APP
        this.ozBlipApp = (OzBlipApplication) getApplication();
        try {
            this.reviewDao = ozBlipApp.getDBHelper().getDao(ReviewEntity.class);
            this.signDao = ozBlipApp.getDBHelper().getDao(SignEntity.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.reviewListView = (ListView) findViewById(R.id.lv_user_drafts_review);
        this.signListView = (ListView) findViewById(R.id.lv_user_drafts_sign);
        this.reviewLayout = (RelativeLayout) findViewById(R.id.rl_user_drafts_review);
        this.signLayout = (RelativeLayout) findViewById(R.id.rl_user_drafts_sign);
        this.commonRight = (LinearLayout) findViewById(R.id.ll_common_right);
        this.tabRb = (RadioGroup) findViewById(R.id.rg_user_drafts_tab);
        this.reviewRb = (RadioButton) findViewById(R.id.rb_user_drafts_review);
        this.signRb = (RadioButton) findViewById(R.id.rb_user_drafts_sign);
        // 初始化先显示评论列表
        this.reviewLayout.setVisibility(0);
        this.signLayout.setVisibility(8);
        this.reviewRb.setChecked(true);
        try {
            reviewList = reviewDao.queryForAll();
            setReviewListAdapter();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置监听
     */
    public void setListeners() {
        View.OnClickListener moreClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_more_layout_setting:
                        Intent settingIntent = new Intent(UserDraftsActivity.this,
                                SettingActivity.class);
                        startActivity(settingIntent);
                        break;
                    case R.id.ll_common_right:
                        if (reviewRb.isChecked()) {
                            Dialog alertDialog = new AlertDialog.Builder(
                                    UserDraftsActivity.this)
                                    .setTitle("提示")
                                    .setMessage("确定删除点评草稿吗？")
                                    .setIcon(android.R.drawable.ic_menu_help)
                                    .setPositiveButton("确定",
                                            new DialogInterface.OnClickListener() {

                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    try {
                                                        reviewDao.deleteBuilder()
                                                                .delete();
                                                    } catch (SQLException e) {
                                                        e.printStackTrace();
                                                    }
                                                    finish();
                                                }
                                            })
                                    .setNegativeButton("取消",
                                            new DialogInterface.OnClickListener() {

                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    dialog.dismiss();
                                                }
                                            }).create();
                            alertDialog.show();

                        } else {
                            Dialog alertDialog = new AlertDialog.Builder(
                                    UserDraftsActivity.this)
                                    .setTitle("提示")
                                    .setMessage("确定删除签到草稿吗？")
                                    .setIcon(android.R.drawable.ic_menu_help)
                                    .setPositiveButton("确定",
                                            new DialogInterface.OnClickListener() {

                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    try {
                                                        signDao.deleteBuilder()
                                                                .delete();
                                                    } catch (SQLException e) {
                                                        e.printStackTrace();
                                                    }
                                                    finish();
                                                }
                                            })
                                    .setNegativeButton("取消",
                                            new DialogInterface.OnClickListener() {

                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    dialog.dismiss();
                                                }
                                            }).create();
                            alertDialog.show();

                        }
                        break;

                    default:
                        break;
                }
            }
        };
        this.commonRight.setOnClickListener(moreClick);
        this.tabRb
                .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if (checkedId == R.id.rb_user_drafts_review) {
                            reviewLayout.setVisibility(0);
                            signLayout.setVisibility(8);
                            try {
                                reviewList = reviewDao.queryForAll();
                                setReviewListAdapter();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            signLayout.setVisibility(0);
                            reviewLayout.setVisibility(8);
                            try {
                                signList = signDao.queryForAll();
                                setSignListAdapter();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
        this.reviewListView
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int position, long arg3) {
                        ReviewEntity review = reviewList.get(position);
                        Intent addReviewI = new Intent();
                        addReviewI.setClass(UserDraftsActivity.this,
                                AddReviewActivity.class);
                        addReviewI.putExtra("reviewInfo", review);
                        startActivity(addReviewI);
                    }
                });
        this.signListView
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int position, long arg3) {
                        SignEntity sign = signList.get(position);
                        Intent addReviewI = new Intent();
                        addReviewI.setClass(UserDraftsActivity.this,
                                AddSignedActivity.class);
                        addReviewI.putExtra("signInfo", sign);
                        startActivity(addReviewI);
                    }
                });
    }

    /**
     * 获取传递的参数
     */
    private void getBundle() {
        // Intent intent = getIntent();
    }

    /**
     * 调用评论列表适配器
     */
    public void setReviewListAdapter() {
        if (CollectionUtil.UnNullAndZero(reviewList)) {
            this.reviewListAdapter = new DraftsReviewListAdapter(
                    UserDraftsActivity.this);
            this.reviewListAdapter.addItems(reviewList);
            this.reviewListView.setAdapter(this.reviewListAdapter);
        }
    }

    /**
     * 调用签到列表适配器
     */
    public void setSignListAdapter() {
        if (CollectionUtil.UnNullAndZero(signList)) {
            this.signListAdapter = new DraftsSignListAdapter(
                    UserDraftsActivity.this);
            this.signListAdapter.addItems(signList);
            this.signListView.setAdapter(this.signListAdapter);
        }
    }
}
